#include "timernode.h"
#include <sys/time.h>


TimerNode::TimerNode(std::shared_ptr<HttpData> requestHttpData, int timeout):deleted(false), httpData(requestHttpData)
{
    struct timeval timer;

    gettimeofday(& timer, nullptr);

    this->expire = (timer.tv_sec % 10000) * 1000 + (timer.tv_usec / 1000) + timeout;


}

TimerNode::TimerNode(const TimerNode &node):httpData(node.httpData)
{

}

TimerNode::~TimerNode()
{
    if (!this->httpData) {
        return;
    }

    this->httpData->handleClose();
}

void TimerNode::update(int timeout)
{
    struct timeval timer;

    gettimeofday(& timer, nullptr);

    this->expire = (timer.tv_sec % 10000) * 1000 + (timer.tv_usec / 1000) + timeout;
}

bool TimerNode::isValid()
{
    struct timeval timer;

    gettimeofday(& timer, nullptr);

    long expire = (timer.tv_sec % 10000) * 1000 + (timer.tv_usec / 1000);

    if (expire < this->expire) {

        return true;
    }

     this->setDelete();

    return false;
}

void TimerNode::cleanRequest()
{
    this->httpData.reset();

    this->setDelete();
}
